package com.music.task;

import com.music.dao.SeckillMapper;
import com.music.entity.Seckill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class AlbumSeckillTask {
    @Autowired
    private SeckillMapper seckillMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0 1 9 7 ?")
    public void setAlbumSeckillToRedis(){
        List<Seckill> seckillGoods = seckillMapper.findAlbumSeckillGoodsByConditions(null);
        seckillGoods.forEach(seckill -> {
//            if(redisTemplate.opsForHash().hasKey("seckillGoods", seckill.getSeckillId())){
//                redisTemplate.opsForHash().delete("seckillGoods",seckill.getSeckillId());
//            }
//            if(redisTemplate.opsForList().indexOf("seckillGoodsList", seckill.getSeckillId()) > 0){
//                redisTemplate.opsForList().remove("seckillGoodsList",1,seckill.getSeckillId());
//            }
            redisTemplate.opsForHash().put("seckillGoods",seckill.getSeckillId(),seckill);
            redisTemplate.opsForList().rightPush("seckillGoodsList",seckill.getSeckillId());
        });
    }
}
